<?
import("izz.service.TransactionService");
import("iforum.common.Util");
import('iforum.common.IforumRedirect');
import('iforum.common.IforumError');
/**
 *  
 * 
 * @package  iforum.service
 * @author   iCer <iCer@justdn.com>
 * @version  $Id: savenewthreadService.php,v 1.1 2005/04/06 04:07:55 icer Exp $
 */
class savenewthreadService extends TransactionService
{

	function execute()
	{
		$context = Singleton::instance('iForumWebAppContext');
		$request = &$context->getRequest();

		$currentuser = $context->getCurrentUser();
		$factory = $context->getDaoFactory();
		
		$tdao = $factory->getThreadDao();
		$fdao = $factory->getForumDao();
		$sdao = $factory->getStatusDao();
		$pdao = $factory->getPostDao();

		$thread = &$request->getParameter('thread');
		
		//get forum
		$forum = $fdao->getForum($thread['fID']);
		
		//add thread
		$guid = Guid::newGuid();
		$guid = $guid->toString();
		
		$thread['tGuid'] = $guid;		
		$thread['uID'] = $currentuser['uID'];
		$thread['uName'] = $currentuser['uName'];
		$thread['lastUID'] = $currentuser['uID'];
		$thread['lastUName'] = $currentuser['uName'];
		$thread['postTime'] = time();
		$thread['lastPostTime'] = time();
		$thread['tPostnum'] = 1;
		
		foreach($thread as $key=>$value)
		{
			$thread[$key] = htmlencode($value);
		}

		$tid = $tdao->addThread($thread);

		//add post
		$guid = Guid::newGuid();
		$guid = $guid->toString();		
		
		$post['pGuid'] = $guid;
		$post = &$request->getParameter('post');
		$post['tID'] = $tid;
		$post['uID'] = $currentuser['uID'];
		$post['pDatetime'] = time();
		$post['uIPAdress'] = &$request->getRemoteAddr();
		
		foreach($post as $key=>$value)
		{
			$post[$key] = htmlencode($value);
		}
		
		$pdao->addPost($post,$forum['fTableID']);
		
		//update forum
		$fdao->updateCount($forum['fID'], 1, 1);
		
		//update global status
		$sdao->updateCount(1,1);
		
		IforumRedirect::go("New reply saved", 
		"index.php?act=thread&amp;fid=".$thread['fID']."&amp;tid=".$tid);
		return true;
	}
}

?>